package cn.lopr.im.api.utils.cos;

import com.tencent.cloud.CosStsClient;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;

import java.util.TreeMap;

/**
 * @author shuangyang7
 * @date 2021/5/20 15:40
 */
public class CosClient {

    private String secretId;
    private String secretKey;
    private int durationSeconds;
    @Value("${cos.config.bucket}")
    private String bucket;
    @Value("${cos.config.region}")
    private String region;

    /**
     * 配置示例：
     * -DSecretId=""
     * -DSecretKey=""
     * -DdurationSeconds=
     */
    private final TreeMap<String, Object> config = new TreeMap<String, Object>() {
        {
            put("allowPrefix", "*");
        }
    };

    void init() {
        this.secretId = System.getProperty("SecretId");
        this.secretKey = System.getProperty("SecretKey");
        this.durationSeconds = Integer.parseInt(System.getProperty("durationSeconds"));

        config.put("bucket", this.bucket);
        config.put("region", this.region);
        config.put("SecretId", this.secretId);
        config.put("SecretKey", this.secretKey);
        config.put("durationSeconds", this.durationSeconds);
    }

    public JSONObject getTempSecret() {
        try {
            String[] allowActions = new String[]{
                    // 简单上传
                    "name/cos:PutObject",
                    // 表单上传、小程序上传
                    "name/cos:PostObject",
                    // 分片上传
                    "name/cos:InitiateMultipartUpload",
                    "name/cos:ListMultipartUploads",
                    "name/cos:ListParts",
                    "name/cos:UploadPart",
                    "name/cos:CompleteMultipartUpload"
            };
            init();
            config.put("allowActions", allowActions);

            //成功返回临时密钥信息，如下打印密钥信息
            return CosStsClient.getCredential(config);
        } catch (Exception e) {
            //失败抛出异常
            e.printStackTrace();
            throw new IllegalArgumentException("no valid secret !");
        }
    }

    public String getBucket() {
        return bucket;
    }

    public void setBucket(String bucket) {
        this.bucket = bucket;
    }

    public String getRegion() {
        return region;
    }

    public void setRegion(String region) {
        this.region = region;
    }

    public String getSecretId() {
        return secretId;
    }

    public void setSecretId(String secretId) {
        this.secretId = secretId;
    }

    public String getSecretKey() {
        return secretKey;
    }

    public void setSecretKey(String secretKey) {
        this.secretKey = secretKey;
    }

    public int getDurationSeconds() {
        return durationSeconds;
    }

    public void setDurationSeconds(int durationSeconds) {
        this.durationSeconds = durationSeconds;
    }

    public TreeMap<String, Object> getConfig() {
        return config;
    }

    public static void main(String[] args) {
        System.out.println(new CosClient().getTempSecret());
    }
}
